import BaseComponent from "./BaseComponent";
import {Button as AntButton, Checkbox, Select} from "antd";
import React from "react";
import SysDataPresenter from "../../.gen/presenter/SysDataPresenter";

export default class RCheckboxGroup extends BaseComponent {

    static defaultProps = {
        dictCode: undefined
    };

    constructor(props) {
        super(props);
        this.state = {
            dictOptions: []
        }
        this.presenter = new SysDataPresenter(this);
        let dictCode = this.props.dictCode;
        if (dictCode&&!this.props.children) {
            this.loadDictOptions(dictCode);
        }
    }

    // 根据字典code查询字典项
    loadDictOptions(dictCode) {
        this.presenter.findDictionaryList({dictField: dictCode}, undefined, (res) => {
            if (res.code===0) {
                let dictOptions = res.data.map(item => ({
                    value: this.props.dictValue ? item['' + this.props.dictValue] : item.dicValue,
                    label: this.props.dictText ? item['' + this.props.dictText] : item.dicName
                }))
                this.setState({dictOptions:dictOptions});
            } else {
                console.error('getDictItems error: : ', res)
            }
        })
    }

    render() {
        let {dictOptions} = this.state;
        let props = {...this.props};
        delete props.dictCode;
        return (
            <Checkbox.Group
                {...props}
                options={dictOptions}
            >
            </Checkbox.Group>
        )
    }
}